﻿using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using WorkFlow.DBProxy;
using WorkFlow.Engine.NSActivity;

namespace WorkFlow.Engine.NSMessage
{
    public class WfCase
    {
        /// <summary>
        ///     获取该用户能够启动 并且状态不为frozen,complete以及terminate的活动列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<string> GetCasesByUser(int userID)
        {
            OracleParameter[] obj =
            {
                new OracleParameter("var_user", OracleDbType.Int32, ParameterDirection.Input) {Value = userID},
                new OracleParameter("actids", OracleDbType.Varchar2, 1000),
                new OracleParameter("isThrowExcetpion", OracleDbType.Int32, ParameterDirection.Output)
            };
            obj[1].Direction = ParameterDirection.Output;
            DB.ExecuteProc("GetActlist", obj);
            if (Convert.ToInt32(obj[2].Value.ToString()) > 0 || string.IsNullOrEmpty(obj[1].Value.ToString()))
                return null; //如果 isThrowExcetpion >0 说明有了异常 那直接返回把
            string[] strs = obj[1].Value.ToString().Split(';');
            int count = strs.Length - 1;
            var caseList = new List<string>();
            for (int i = 0; i < count; i++)
            {
                caseList.Add(strs[i]);
            }
            return caseList;
        }

        /// <summary>
        ///     获取内存中空闲的(可用的活动)活动ID列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<int> GetMemoryAvailableCaseList(int userID)
        {
            ReadOnlyCollection<WfActivity> actsList = WfActivityController.GetReadyActList();
            if (actsList.Count < 1) return null;

            var list = new List<int>();
            int index = 0;

            int count = actsList.Count;
            for (int i = 0; i < count; i++)
            {
                if (actsList[i].IsUserHasPrivilege(userID))
                {
                    list[index++] = actsList[i].ID;
                }
            }
            return list;
        }
    }
}